package fair.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import fair.constant.MessageConstant;
import fair.constant.RedisConstant;
import fair.entity.PageResult;
import fair.entity.QueryPageBean;
import fair.entity.Result;
import fair.pojo.CheckGroup;
import fair.pojo.Setmeal;
import fair.service.SetMealService;
import fair.utils.QiNiuUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import redis.clients.jedis.JedisPool;

import java.util.List;
import java.util.UUID;

/**
 * @author fair_wu
 * @version v1.0
 * @date 2021-01-26 下午 13:01
 */
@RestController
@RequestMapping("/setMeal")
public class SetMealController {

    @Reference
    private SetMealService service;

    @Autowired
    private JedisPool pool;

    @PreAuthorize("hasAuthority('SETMEAL_ADD')")
    @RequestMapping("/upload")
    public Result upload(MultipartFile imgFile){
        String originalFilename = imgFile.getOriginalFilename();
        String fileName = null;
        if (originalFilename != null) {
            int index = originalFilename.lastIndexOf('.');
            String suffix = originalFilename.substring(index);
            fileName = UUID.randomUUID().toString() + suffix;
        }
        try {
            QiNiuUtils.upload(imgFile.getBytes(), fileName);
            pool.getResource().sadd(RedisConstant.SETMEAL_PIC_RESOURCES, fileName);
            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS, fileName);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    @PreAuthorize("hasAuthority('SETMEAL_ADD')")
    @PostMapping("/add")
    public Result add(@RequestBody Setmeal setmeal, Integer[] checkGroupIds){
        try {
            service.add(setmeal, checkGroupIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKGROUP_FAIL);
        }
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPage){
        return service.pageQuery(queryPage.getCurrentPage(), queryPage.getPageSize(), queryPage.getQueryString());
    }

    @PreAuthorize("hasAuthority('SETMEAL_DELETE')")
    @DeleteMapping("/delete")
    public Result delete(Integer id) {
        try {
            service.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_SETMEAL_FAIL);
        }
        return new Result(true, MessageConstant.DELETE_SETMEAL_SUCCESS);
    }

    @PreAuthorize("hasAuthority('CHECKITEM_QUERY')")
    @GetMapping("/findById")
    public Result findById(Integer id){
        try {
            Setmeal setmeal = service.findById(id);
            return new Result(true, MessageConstant.QUERY_SETMEAL_SUCCESS, setmeal);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.QUERY_SETMEAL_FAIL);
        }
    }

    @PreAuthorize("hasAuthority('SETMEAL_EDIT')")
    @GetMapping("/findCheckGroupIdsBySetMealId")
    public Result findCheckItemIdsByCheckGroupId(Integer id){
        try {
            List<Integer> ids = service.findCheckGroupIdsBySetMealId(id);
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS, ids);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(true, MessageConstant.QUERY_CHECKGROUP_FAIL);
        }
    }

    @PreAuthorize("hasAuthority('SETMEAL_EDIT')")
    @PutMapping("/edit")
    public Result edit(@RequestBody Setmeal setmeal, Integer[] checkGroupIds){
        try {
            service.edit(setmeal, checkGroupIds);
            return new Result(true, MessageConstant.EDIT_SETMEAL_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_SETMEAL_FAIL);
        }
    }
}
